package com.icomvn.game.pikachu.play;

import java.util.Random;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.icomvn.game.pikachu.R;
import com.icomvn.game.pikachu.config.Config;
import com.icomvn.game.pikachu.music.PlayMusic;
import com.icomvn.game.pikachu.object.ActivityInterface;
import com.icomvn.game.pikachu.object.HandlerObject;
import com.icomvn.game.pikachu.object.OnFocusChangeListenerPikachu;
import com.icomvn.game.pikachu.utility.Save;
import com.icomvn.game.pikachu.utility.TimeCount;
import com.icomvn.game.pikachu.utility.Utility;
import com.icomvn.game.pikchu.solution.Solution;
import com.icomvn.game.pikchu.solution.Variable;

public class PlayPikachu extends Activity implements ActivityInterface,
		OnClickListener {
	int x[], y[];
	int x1 = -1, y1 = -1;
	int pikachuWidth = 1;
	int pikachuHeigth = 1;
	int countlistLevel = 0;
	int numOrigen, Width, Height, difficult;
	int numChange = 3;
	int next, key, currentTime;
	float animY, height;
	boolean isFinish = false;
	static int lengthX = Config.lengthX;
	static int lengthY = Config.lengthY;
	static int countSearch = 0;
	static int countOrigen = 0;
	static int mode = 0;
	static int step = 0;
	public boolean isWaiting;
	public Algorithms algorithms;
	public static int cost = 1;
	public static int score, bonus;
	public static int level;
	public static String road = "";
	public static int startX, startY, endX, endY;
	public int result[][] = new int[lengthX][lengthY];
	public static boolean isRunning = false;
	LinearLayout bgPlay;
	Activity activity;
	TextView bloodText, scoreText;
	ImageView background, sound, music, untime, bgsound, bgtime, bgmenu,
			bgdiem, bgmang, dongy,thoat;
	ImageButton pikachu[][];
	ImageButton pika1, pika2;
	LinearLayout.LayoutParams params;
	FrameLayout bg;
	SharedPreferences setup;
	SharedPreferences.Editor editor;
	ViewGroup alert;
	Button alertOk, alertCancel, back;
	TextView alertMessage;
	Random rand;
	ProgressDialog progress;
	HandlerObject handler;
	PlayPikachu playActivity;
	PlayMusic mp;
	Animation helpAnimation1, helpAnimation2, wait1, wait2, animation, wait;
	TimeCount countThread = null;
	TranslateAnimation ani;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.play);

		Display display = ((WindowManager) getBaseContext().getSystemService(
				Context.WINDOW_SERVICE)).getDefaultDisplay();

		// lay chieu rong cua man hinh
		Width = display.getWidth();
		// lay chieu cao cua man hinh
		Height = display.getHeight();
		height = Height;
		if (Width == 320 && Height == 240) {
			setContentView(R.layout.play240320);
		}
		if (Width == 480 && Height == 320) {
			setContentView(R.layout.play320480);
		}
		if (Width == 1280 && Height == 800) {
			setContentView(R.layout.play8001280);
		}
		if (Width == 1280 && Height == 720) {
			setContentView(R.layout.play7201280);
		}
		bg = (FrameLayout) findViewById(R.id.bgall);
		sound = (ImageView) findViewById(R.id.sound);
		sound.setOnClickListener(this);
		music = (ImageView) findViewById(R.id.music);
		music.setOnClickListener(this);
		dongy = (ImageView) findViewById(R.id.dongy);
		dongy.setOnClickListener(this);
		thoat = (ImageView) findViewById(R.id.exit3);
		thoat.setOnClickListener(this);
		// bgsound = (ImageView)findViewById(R.id.bgam);

		mp = new PlayMusic(this);
		Intent i = getIntent();
		boolean playMu = i.getBooleanExtra("mu", false);
		if (playMu) {
			music.setImageResource(R.drawable.tatnhac);
			mp.playMusic = false;
			sound.setImageResource(R.drawable.tatam);
			mp.playSound = false;
		} else {
			mp.run();
		}
		difficult = i.getIntExtra("diff", 0);
		Log.d("diff", Integer.toString(difficult));
		activity = this;
		playActivity = this;
		untime = (ImageView) findViewById(R.id.untime);
		setlayout(untime, 25, -120, 0, 0);
		// background = (ImageView) findViewById(R.id.playbackground);
		// bgtime = (ImageView)findViewById(R.id.time);
		// bgmenu = (ImageView)findViewById(R.id.bgmenu);
		scoreText = (TextView) findViewById(R.id.score);
		bloodText = (TextView) findViewById(R.id.blood);

		alert = (ViewGroup) findViewById(R.id.playalert);
		alertMessage = (TextView) findViewById(R.id.text2);

		pikachu = new ImageButton[lengthX][lengthY];

		bgPlay = (LinearLayout) findViewById(R.id.playviewgroup2);
		road = "";
		rand = new Random();
		algorithms = new Algorithms();
		creatNewGame();
		Save.playGame = this;

		// setupListener();
		// setupAnimation();
		// if (!isRunning)
		// currentTime = 60*5;
		// countThread = new TimeCount( 10000, 1000, this);
		// Save.count = countThread;
		// if (mode != 5 && mode != 8)
		// countThread.start();
		// setup = activity.getSharedPreferences("pikachu_icom",
		// // Activity.MODE_PRIVATE);
		// editor = setup.edit();
		// try {
		// Save.mp.stop();
		// Save.play.stop();
		// } catch (Exception e) {
		// }
		// if (Save.music) {
		// mp = new PlayMusic(this);
		// mp.start();
		// Save.play = mp;
		// }

	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		if (hasFocus) {
			animationTime(Width / 32, -Height / 4, Width / 32, Height / 4,
					300000);
		}
	}

	public void setupAnimation() {
		wait = AnimationUtils.loadAnimation(this, R.anim.static_button);
		wait.setAnimationListener(new AnimationListener() {
			public void onAnimationEnd(Animation _animation) {
				pikachu[startX][startY].setImageResource(R.drawable.tranparent);
				for (int i = 0; i < x.length; i++) {
					pikachu[x[i]][y[i]].setImageResource(R.drawable.tranparent);
				}
				pikachu[endX][endY].setImageResource(R.drawable.tranparent);
			}

			public void onAnimationRepeat(Animation _animation) {
			}

			public void onAnimationStart(Animation _animation) {
			}
		});
		wait1 = AnimationUtils.loadAnimation(this, R.anim.static_button);
		wait1.setAnimationListener(new AnimationListener() {
			public void onAnimationEnd(Animation _animation) {
				pika1.startAnimation(helpAnimation1);
			}

			public void onAnimationRepeat(Animation _animation) {
			}

			public void onAnimationStart(Animation _animation) {
			}
		});
		wait2 = AnimationUtils.loadAnimation(this, R.anim.static_button);
		wait2.setAnimationListener(new AnimationListener() {
			public void onAnimationEnd(Animation _animation) {
				pika2.startAnimation(helpAnimation2);
			}

			public void onAnimationRepeat(Animation _animation) {
			}

			public void onAnimationStart(Animation _animation) {
			}
		});
	}

	public void nextLevelStep2() {
		// mode = 0;
		level++;
		isFinish = false;
		isRunning = false;
		setupPikachu();
		applyMatrix();
		isRunning = true;
		animationTime(Width / 32, -Height / 4, Width / 32, Height / 4, 300000);
		countThread.setmillisInFuture(300 * 1000);
		Save.count = countThread;
		countThread.start();
	}

	public boolean help() {
		boolean stop = false;
		for (int i1 = 1; i1 < lengthX - 1; i1++) {
			for (int j1 = 1; j1 < lengthY - 1; j1++) {
				if (result[i1][j1] != 0)
					for (int i2 = i1; i2 < lengthX - 1; i2++) {
						for (int j2 = 1; j2 < lengthY - 1; j2++) {
							if (checkTruePikachuOnly(i1, j1, i2, j2)) {
								stop = true;
								break;
							}
						}
						if (stop)
							break;
					}
				if (stop)
					break;
			}
			if (stop)
				break;
		}
		return stop;
	}

	public boolean checkTruePikachuOnly(int x1, int y1, int x2, int y2) {
		if (result[x1][y1] == result[x2][y2]) {
			// check tồn tại đường đi hợp lệ
			cost = 20000;
			if (Solution.checkRoad(x1, y1, 0, 0, x2, y2, "", result, lengthX,
					lengthY, 0))
				return true;
		}
		return false;
	}

	public void origen() {
		int resultMid[] = null;
		int num = 0;
		for (int i = 0; i < lengthX; i++)
			for (int j = 0; j < lengthY; j++) {
				if (result[i][j] != 0)
					num++;
			}
		if(level<3){
			resultMid = Utility.getRandomSession_6(num);
		}else {
			resultMid = Utility.getRandomSession_4(num);
		}
		int count = 0;
		for (int i = 0; i < lengthX; i++)
			for (int j = 0; j < lengthY; j++) {
				pikachu[i][j] = (ImageButton) findViewById(Variable.pikachuId[i][j]);
				pikachu[i][j].setLayoutParams(params);
				if (i>0 && (j-difficult)>0 && i<(lengthX - 1)
						&& (j+difficult)<(lengthY - 1))
					if (result[i][j] != 0) {
						result[i][j] = resultMid[count];
						pikachu[i][j]
								.setBackgroundResource(Variable.pikachuImgId[resultMid[count]]);
						count++;
						String iMid = "";
						String jMid = "";
						iMid = String.valueOf(i);
						if (i < 10)
							iMid = "0" + String.valueOf(i);
						else
							iMid = String.valueOf(i);
						if (j < 10)
							jMid = "0" + String.valueOf(j);
						else
							jMid = String.valueOf(j);
						pikachu[i][j].setContentDescription(iMid + jMid);
						pikachu[i][j].setOnClickListener(new OnClickListener() {
							public void onClick(View v) {
								clickBG((ImageButton) v);
							}
						});
					}
			}
	}

	public void setupPikachu() {
		int resultMid[] = null;
		if (!isRunning) {
			step = 0;
			if(level<3 && difficult==0){
				resultMid = Utility.getRandomSession_6((lengthX - 2)
					* (lengthY - 2-(2*difficult)));
			}else {
				resultMid = Utility.getRandomSession_4((lengthX - 2)
						* (lengthY - 2));
			}
//			resultMid=Utility.getRandomSession_4((lengthX - 2) * (lengthY - 2-(2*difficult)));
		}
		// bgPlay.setBackgroundResource(Variable.imageId[level -1]);
		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		int height = this.getWindowManager().getDefaultDisplay().getHeight();
		pikachuWidth = width / 16;
		pikachuHeigth = 2 * height / 19;
		params = new LinearLayout.LayoutParams(pikachuWidth, pikachuHeigth);
		int count = 0;
		for (int i = 0; i < lengthX; i++)
			for (int j = 0; j < lengthY; j++) {
				pikachu[i][j] = (ImageButton) findViewById(Variable.pikachuId[i][j]);
				pikachu[i][j].setLayoutParams(params);
				if (i>0 && (j-difficult)>0 && i<(lengthX - 1)
						&& (j+difficult)<(lengthY - 1)) {
					// tập kết quả ánh xạ
					if (!isRunning) {
						result[i][j] = resultMid[count];
					}
					count++;
					String iMid = "";
					String jMid = "";
					iMid = String.valueOf(i);
					if (i < 10)
						iMid = "0" + String.valueOf(i);
					else
						iMid = String.valueOf(i);
					if (j < 10)
						jMid = "0" + String.valueOf(j);
					else
						jMid = String.valueOf(j);
					pikachu[i][j].setContentDescription(iMid + jMid);
					pikachu[i][j].setOnClickListener(new OnClickListener() {
						public void onClick(View v) {
							clickBG((ImageButton) v);
						}
					});
					pikachu[i][j]
							.setOnFocusChangeListener(new OnFocusChangeListenerPikachu());
				}
			}
	}

	public void applyMatrix() {
		for (int i = 0; i < lengthX; i++)
			for (int j = 0; j < lengthY; j++) {
					// tập kết quả ánh xạ
					if (result[i][j] == 0) {
						pikachu[i][j]
								.setBackgroundResource(R.drawable.tranparent);
						pikachu[i][j].setClickable(false);
					} else {
						pikachu[i][j]
								.setBackgroundResource(Variable.pikachuImgId[result[i][j]]);
					}
			}
	}

	public void successGame(boolean success) {
		// if (score > setup.getLong("score", 0)) {
		// editor.putLong("score", score);
		// editor.commit();
		// }
		if (success) {
			alert("Bạn đã thắng với số điểm: " + Integer.toString(score), 3,
					true);
		} else {
			isFinish = true;
			alert("Hết giờ,điểm của bạn là: " + Integer.toString(score), 3,
					true);
		}
	}

	public boolean checkTruePikachu(int x1, int y1, int x2, int y2) {
		if (result[x1][y1] == result[x2][y2]) {
			// check tồn tại đường đi hợp lệ
			for (int i = 1; i <= 15; i++) {
				cost = i;
				if (Solution.checkRoad(x1, y1, 0, 0, x2, y2, "", result,
						lengthX, lengthY, 0))
					return true;
			}
			cost = 20000;
			if (Solution.checkRoad(x1, y1, 0, 0, x2, y2, "", result, lengthX,
					lengthY, 0))
				return true;
		}
		return false;
	}

	public void onSuccess() {
		// x = Parser.getPropertyToInt(road, "<x>", "</x>");
		// y = Parser.getPropertyToInt(road, "<y>", "</y>");
		// pikachu[startX][startY].setImageResource(R.drawable.connect);
		// for (int i = 0; i < x.length; i++) {
		// pikachu[x[i]][y[i]].setImageResource(R.drawable.connect);
		// }
		// pikachu[endX][endY].setImageResource(R.drawable.connect);
		step++;
		score += 2;
		if (score < 1000) {
			if (score < 100) {
				if (score < 10) {
					scoreText.setText("000" + String.valueOf(score));
				} else {
					scoreText.setText("00" + String.valueOf(score));
				}
			} else {
				scoreText.setText("0" + String.valueOf(score));
			}
		} else {
			scoreText.setText(String.valueOf(score));
		}

	}

	@Override
	public void onBackPressed() {
		alert("Thoát nhé???", 1, true);
		// super.onBackPressed();
	}

	public void setupCheck(ImageButton pika) {
		int i = 0, j = 0;

		String descript = pika.getContentDescription().toString();
		i = Integer.valueOf("" + descript.charAt(0) + descript.charAt(1));
		j = Integer.valueOf("" + descript.charAt(2) + descript.charAt(3));
		if (x1 != -1) {
			if (x1 != i || y1 != j)
				if (checkTruePikachu(x1, y1, i, j)) {
					mp.checkTrue();
					next++;
					startX = x1;
					startY = y1;
					endX = i;
					endY = j;
					Log.e("print", "road:" + road);
					// invisible 2 pikachu
					pikachu[i][j].setBackgroundResource(R.drawable.tranparent);
					pikachu[x1][y1]
							.setBackgroundResource(R.drawable.tranparent);
					pikachu[x1][y1].setImageResource(R.drawable.tranparent);
					pikachu[i][j].setImageResource(R.drawable.tranparent);
					onSuccess();
					// save
					result[i][j] = 0;
					result[x1][y1] = 0;
					// constructor
					x1 = -1;
					y1 = -1;
					if(difficult==0){
						algorithms.fixMatrix(result, level);
						applyMatrix();
						checkPassLevel(48);
					}else if(difficult==1){
						algorithms.fixMatrix_1(result, level);
						applyMatrix();
						checkPassLevel(40);
					}else if(difficult==2){
						algorithms.fixMatrix_2(result, level);
						applyMatrix();
						checkPassLevel(32);
					}

				} else {
					// constructor
					mp.playMiss();
					pikachu[x1][y1].setImageResource(R.drawable.tranparent);
					pikachu[i][j].setImageResource(R.drawable.tranparent);
					x1 = -1;
					y1 = -1;
				}
		} else {
			x1 = i;
			y1 = j;
		}

	}
	public void checkPassLevel(int numPi){
		if ((next % numPi) == 0) {
			bonus = countThread.timeSave;
			score += bonus;
			if (score < 1000) {
				if (score < 100) {
					if (score < 10) {
						scoreText.setText("000" + String.valueOf(score));
					} else {
						scoreText.setText("00" + String.valueOf(score));
					}
				} else {
					scoreText.setText("0" + String.valueOf(score));
				}
			} else {
				scoreText.setText(String.valueOf(score));
			}
			if (next == numPi*9) {
				successGame(true);
			} else {
				alert("Điểm thưởng: " + Integer.toString(bonus), 2,
						false);
			}
		} else {

			while (!help()) {
				if (numOrigen > 0) {
					origen();
					numOrigen--;
					bloodText.setText("0"
							+ Integer.toString(numOrigen));
				} else {
					alert("Thua rồi!!!", 3, true);
					break;
				}
			}
		}
	}

	public void clickBG(TextView v, boolean invi, boolean high) {
		final TextView v1 = v;
		Animation clickButton = AnimationUtils.loadAnimation(this,
				R.anim.static_button);
		clickButton.setAnimationListener(new AnimationListener() {
			public void onAnimationEnd(Animation _animation) {
				v1.setTextColor(Color.BLUE);
			}

			public void onAnimationRepeat(Animation _animation) {
			}

			public void onAnimationStart(Animation _animation) {
				v1.setTextColor(Color.WHITE);
			}
		});
		v1.startAnimation(clickButton);
	}

	public void alert(String message, int key, boolean isCancel) {
		this.key = key;
		lock();
		bg.setBackgroundResource(R.drawable.bg_denmo);
		alertMessage.setText(message);
		currentTime = countThread.timeSave;
		countThread.cancel();
		ani.cancel();
		ani.reset();
		animY = ((Height / 4) - ((height / 600)) * currentTime);
		setlayout(untime, Width / 32, (int) animY, 0, 0);
		if (!isCancel) {
			thoat.setVisibility(View.GONE);
		}else{
			thoat.setVisibility(View.VISIBLE);
		}
		switch (key) {
		case 1:
			dongy.setImageResource(R.drawable.dongy);
			break;

		case 2:
			dongy.setImageResource(R.drawable.popup_choitiep);
			break;
		case 3:
			dongy.setImageResource(R.drawable.choi_lai);
			break;

		default:
			break;
		}
		alert.setVisibility(View.VISIBLE);
	}

	public void alertFinish() {
		// TODO Auto-generated method stub
		try {
			progress.dismiss();
		} catch (Exception e) {
		}
		Log.e("handle", "start alert");
		if (Save.titleErro.compareTo("") != 0) {
			if (Save.finish) {
				mode = 1;
			} else {
				mode = 4;
			}
			alert(Save.titleErro + "." + Save.contentErro, 0, false);
			Save.finish = false;
		}
	}

	public void continueGame() {
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		countSearch = 0;
		countOrigen = 0;
		isFinish = false;
		level = 1;
		score = 0;
		mode = 0;
		result = new int[lengthX][lengthY];
		isRunning = false;
		step = 0;
		// currentTime = maxTime[level - 1];
		try {
			Save.mp.stop();
			// Save.play.stop();
		} catch (Exception e) {
		}
		try {
			Save.count.cancel();
		} catch (Exception e) {
			Log.e("erro", e.toString());
		}
		road = "";
		super.finish();
	}

	public void timeEnd() {
		successGame(false);
	}

	public void clickBG(Button v) {
		final Button v1 = v;
		Animation clickButton = AnimationUtils.loadAnimation(this,
				R.anim.static_button);
		clickButton.setAnimationListener(new AnimationListener() {
			public void onAnimationEnd(Animation _animation) {
				v1.setBackgroundResource(R.drawable.buttonhigh);
				alert.setVisibility(View.GONE);
				mp.stopMusic();
				activity.finish();

			}

			public void onAnimationRepeat(Animation _animation) {
			}

			public void onAnimationStart(Animation _animation) {
				v1.setBackgroundResource(R.drawable.buttonhighselected);
			}
		});
		v1.startAnimation(clickButton);
	}

	public void clickBG(ImageButton v) {
		final ImageButton v1 = v;
		int i = 0, j = 0;
		mp.chose();
		String descript = v1.getContentDescription().toString();
		i = Integer.valueOf("" + descript.charAt(0) + descript.charAt(1));
		j = Integer.valueOf("" + descript.charAt(2) + descript.charAt(3));
		if (result[i][j] != 0) {
			Animation clickButton = AnimationUtils.loadAnimation(this,
					R.anim.static_button);
			// clickButton.setDuration(10);
			clickButton.setAnimationListener(new AnimationListener() {
				public void onAnimationEnd(Animation _animation) {
					setupCheck(v1);
				}

				public void onAnimationRepeat(Animation _animation) {
				}

				public void onAnimationStart(Animation _animation) {
					v1.setImageResource(R.drawable.connect);
				}
			});
			v1.startAnimation(clickButton);

		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.music:
			if (mp.playMusic) {
				music.setImageResource(R.drawable.tatnhac);
				mp.stopMusic();
				mp.playMusic = false;
			} else {
				music.setImageResource(R.drawable.nhac);
				mp.playMusic = true;
				mp.startMusic();
			}
			break;
		case R.id.sound:
			if (mp.playSound) {
				sound.setImageResource(R.drawable.tatam);
				mp.playSound = false;
			} else {
				sound.setImageResource(R.drawable.am);
				mp.playSound = true;
			}
			break;

		case R.id.dongy:
			bg.setBackgroundResource(R.drawable.tranparent);
			alert.setVisibility(View.GONE);
			if (key == 1) {
				mp.stopMusic();
				activity.finish();
			}
			if (key == 2) {
				nextLevelStep2();
				unLock();
			}
			if (key == 3) {
				creatNewGame();
				unLock();
			}
			break;

		case R.id.exit3:
			if (key == 1) {
				bg.setBackgroundResource(R.drawable.tranparent);
				alert.setVisibility(View.GONE);
				Log.d("Y2", Float.toString(animY));
				animationTime(Width / 32, animY, Width / 32, Height / 4,
						currentTime * 1000);
				Log.d("time2", Integer.toString(currentTime));
				countThread.setmillisInFuture(currentTime * 1000);
				Save.count = countThread;
				countThread.start();
				unLock();
			} else {
				bg.setBackgroundResource(R.drawable.tranparent);
				alert.setVisibility(View.GONE);
				mp.stopMusic();
				activity.finish();
			}
			break;
		default:
			break;
		}

	}

	private void setlayout(View v, int left, int top, int right, int bottom) {
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				Math.round(v.getLayoutParams().width), Math.round(v
						.getLayoutParams().height));
		params.setMargins(left, top, right, bottom);
		v.setLayoutParams(params);

	}

	private void animationTime(float fromX, float fromY, float toX, float toY,
			int dur) {
		ani = new TranslateAnimation(fromX, toX, fromY, toY);
		ani.setDuration(dur);
		ani.setFillAfter(true);
		untime.startAnimation(ani);
	}

	private void creatNewGame() {
		level = 1;
		score = 0;
		scoreText.setText(Integer.toString(score));
		numOrigen = 10;
		bloodText.setText(Integer.toString(numOrigen));
		next = 0;
		isRunning = false;
		setupPikachu();
		applyMatrix();
		isRunning = true;
		countThread = new TimeCount(300000, 1000, this);
		Save.count = countThread;
		countThread.start();
		animationTime(Width / 32, -Height / 4, Width / 32, Height / 4, 300000);

	}

	private void lock() {
		for (int i = 1; i < lengthX - 1; i++) {
			for (int j = 1; j < lengthY - 1; j++) {
				pikachu[i][j].setClickable(false);
			}
		}
		sound.setClickable(false);
		music.setClickable(false);
	}

	private void unLock() {
		for (int i = 1; i < lengthX - 1; i++) {
			for (int j = 1; j < lengthY - 1; j++) {
				if (result[i][j] != 0) {
					pikachu[i][j].setClickable(true);
				}
			}
		}
		sound.setClickable(true);
		music.setClickable(true);
	}

	// private void resize(){
	// Display display = ((WindowManager) getBaseContext().getSystemService(
	// Context.WINDOW_SERVICE)).getDefaultDisplay();
	//
	// // lay chieu rong cua man hinh
	// int Width = display.getWidth();
	// // lay chieu cao cua man hinh
	// int Height = display.getHeight();
	//
	// ResizeImage.resizeImage(bgsound, 4*Height/19, Width/16,
	// R.drawable.bg_am);
	// ResizeImage.resizeImage(music, Height/16, Width/25, R.drawable.nhac);
	// ResizeImage.resizeImage(sound, Height/16, Width/25, R.drawable.am);
	//
	// ResizeImage.resizeImage(bgdiem, 2*Height/15, Width/8, R.drawable.diem);
	// ResizeImage.resizeImage(bgmang, 2*Height/15, Width/8, R.drawable.mang);
	// ResizeImage.resizeImage(bgmenu, Height, Width/8,
	// R.drawable.bg_menuchoi1);
	// ResizeImage.resizeImage(bgtime, Height/2, Width/16,
	// R.drawable.color_time);
	// ResizeImage.resizeImage(untime, Height/2, Width/16,
	// R.drawable.color_untime);
	//
	//
	//
	// }

}
